package cn.gson.oasys.controller;

import cn.gson.oasys.controller.AouUserControllerlwx.VerifyCodeUtils;
import cn.gson.oasys.pojo.AoaDept;
import cn.gson.oasys.pojo.AoaPosition;
import cn.gson.oasys.pojo.AoaRole;
import cn.gson.oasys.pojo.AoaUser;
import cn.gson.oasys.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by ASUS on 2020/11/26.
 * 目的：用于用户验证登录
 * 操作： 刘雯欣12  ssss
 */
@Controller
public class AoaUserController {

    @Resource
    private AoaUserService aoaUserService;
    @Resource
    private AoaSysMenuService aoaSysMenuService;
    @Resource
    private AoaDeptService aoaDeptService;
    @Resource
    private AoaPositionService aoaPositionService;
    @Resource
    private AoaRoleService aoaRoleService;

    public static final String CAPTCHA_KEY = "session_captcha"; //验证码

    @RequestMapping("/")
    public String toLogin(){
        return "login/login";
    }

    //刘雯欣，通过姓名和密码验证登录，
    @RequestMapping("getByNameAndPassword")
    @ResponseBody
    public Object getByNameAndPassword(String userName, String password, Model model){
        //Shiro获取登录数据
        Subject subject=SecurityUtils.getSubject();
        //Shiro封装用户的登录数据
        UsernamePasswordToken token= new UsernamePasswordToken(userName,password);
        //Shiro验证
        try{
            subject.login(token);   //Shiro执行登录方法，如果没有异常就说明OK了
            //return "index";
        }catch (UnknownAccountException e){    //如果用户名不存在
            model.addAttribute("msg","用户名错误");
            //return "login";
        }catch (IncorrectCredentialsException e){ ////如果用户名不存在
            model.addAttribute("msg2","密码错误");
            //return "login";
        }

//        aoaUser=aoaUserService.getByNameAndPassword(aoaUser);
//        session.setAttribute("user",aoaUser);
   //     System.out.println(aoaUser);
        return aoaUserService.getByNameAndPassword(userName,password);
    }


    @RequestMapping("login")
    public String login(){
        return "login/login";
    }
    @RequestMapping("index")
    public String index(){
        return "index/index";
    }

    //404权限控制
    @RequestMapping("404")
    public String ss(){
        return "Shiro/401";
    }


    @RequestMapping("getAllMenu")
    @ResponseBody
    public Object getAllMenu() {
        return aoaSysMenuService.geyAllFirst();
    }

    @RequestMapping("getAllUserByDepeId")
    @ResponseBody
    //根据部门查询 部门下的所有人的用户名、真实名、职位--WJN
    public Object getAllUserByDepeId(Integer deptId){
        List<AoaUser> aoaUserList = aoaUserService.getAllUserByDepeId(deptId);
        return aoaUserList;
    }
    @RequestMapping("getAllAoaUser")
    @ResponseBody
    //查询全部员工 分页显示 模糊查询 --》WJN
    public Object getAllAoaUsers(@Param("deptName") String deptName,
                                 @Param("realName") String realName,
                                 @Param("userName") String userName,
                                 @Param("roleName") String roleName,
                                 @Param("userTel") String userTel,
                                 @Param("salary") Float salary,
                                 @Param("pageNo") Integer pageNo,
                                 @Param("pageSize") Integer pageSize){
        PageHelper.offsetPage((pageNo-1)*pageSize,pageSize);
        List<AoaUser> aoaUserLists = aoaUserService.getAllAoaUser(deptName,realName,userName,roleName,userTel,salary);
        PageInfo<AoaUser> pageInfo = new PageInfo<AoaUser>(aoaUserLists);
        return pageInfo;
    }
    //查询全部部门 用于用户管理中的 用户添加-》WJN
    @RequestMapping("addAoaUserDept")
    @ResponseBody
    public Object addAoaUserDept(){
        List<AoaDept> aoaDeptList = aoaDeptService.getAllAoaDept();
        return aoaDeptList;
    }
    //查询全部职位 用于用户管理中的 用户添加-》WJN
    @RequestMapping("addAoaUserPosition")
    @ResponseBody
    public Object addAoaUserPosition(){
        List<AoaPosition> aoaPositionList = aoaPositionService.getALLAoaPosotion();
        return aoaPositionList;
    }
    //查询全部角色 用于用户管理中的 用户添加-》WJN
    @RequestMapping("addAoaUserRole")
    @ResponseBody
    public Object addAoaUserRole(){
        List<AoaRole> aoaRoleList = aoaRoleService.getAoaRoleList();
        return aoaRoleList;
    }
    //新增员工 --》WJN
    @RequestMapping("addAoaUsers")
    @ResponseBody
    public Object addAoaUsers(AoaUser aoaUser){
        int i = aoaUserService.addAoaUser(aoaUser);
        return i;
    }
    //根据userId 查询 用于修改表单绑定值--》WJN
    @RequestMapping("getAoaUserByUserId")
    @ResponseBody
    public Object getAoaUserByUserId(Long userId){

        AoaUser aoaUser = aoaUserService.getByUserId(userId);
        return aoaUser;
    }
    //根据 userId 修改user数据
    @RequestMapping("getUpdateByUserId")
    @ResponseBody
    public Object getUpdateByUserId(@Param("aoaUser") AoaUser aoaUser){
        int i = aoaUserService.updateUserId(aoaUser);
        return i;
    }
    //根据 userId 删除user数据--》WJN
    @RequestMapping("getDeleteByUserId")
    @ResponseBody
    public Object getDeleteByUserId(Long userId){
        int i = aoaUserService.deleteAoaUser(userId);
        return i;
    }


    //生成验证码  2020-12-02
    @RequestMapping("captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        // 生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);

        // 生成图片
        int w = 135, h = 40;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
        System.out.println(verifyCode.toLowerCase());

        // 将验证码存储在session以便登录时校验
        session.setAttribute(CAPTCHA_KEY, verifyCode.toLowerCase());
    }


    //验证码判断 12-02 刘雯欣
    @RequestMapping("checkcode")
    @ResponseBody
    public Object checkCode(String code,HttpSession session){
        System.out.println(code.toLowerCase());
        System.out.println(session.getAttribute(CAPTCHA_KEY));
        if(session.getAttribute(CAPTCHA_KEY).equals(code.toLowerCase())){
            return "ok";
        }else {
            return "no";
        }
    }
}

